import Vue from "vue";
import VueRouter from "vue-router";
import Home from "@/views/Home.vue";
import Login from "@/views/Login.vue";
import Main from "@/views/Main";
import Cookies from "js-cookie";
Vue.use(VueRouter);

const routes = [
  // {
  //   path: "/",
  //   redirect: "/login",
  // },
  {
    path: "/login",
    component: Login,
  },
  // {
  //   path: "/home",
  //   component: Home,
  // },
  {
    path: "/",
    name: "Main",
    component: Main,
    redirect: "/home", // 重定向
    children: [
      {
        path: "/home",
        name: "首页",
        component: () => import("@/views/Home.vue"),
      },
      {
        path: "/raise-dust",
        name: "扬尘设备管理",
        component: () => import("@/views/project/RaiseDust.vue"),
      },
      {
        path: "/video",
        name: "视频监控设备管理",
        component: () => import("@/views/project/VideoManager.vue"),
      },
      {
        path: "/stink",
        name: "恶臭监测数据",
        component: () => import("@/views/Stink.vue"),
      },
      {
        path: "/voc",
        name: "voc监测数据",
        component: () => import("@/views/Vocs.vue"),
      },
    ],
  },
];

const router = new VueRouter({
  mode: "hash",
  routes,
});

router.beforeEach((to, from, next) => {
  if (!Cookies.get("token") && to.path !== "/login") {
    next({
      path: "/login",
    });
  } else {
    next();
  }
});

export default router;
